/*
 * Tools.cpp
 *
 *  Created on: 10/01/2011
 *      Author: Nenem
 */

#include "Tools.h"

/*
 * Point2D.cpp
 *
 *  Created on: 16/04/2010
 *      Author: Nenem
 */

string getPath() {
	char cCurrentPath[FILENAME_MAX];
	GetCurrentDir(cCurrentPath, sizeof(cCurrentPath));
	return string(cCurrentPath) + "\\Data\\";
}

Point2D::Point2D() {
	this->x = 0;
	this->y = 0;

}

Point2D::Point2D(int x, int y) {
	this->x = x;
	this->y = y;
}

Point2D::~Point2D() {
	// TODO Auto-generated destructor stub
}

bool Point2D::operator==(const Point2D& other) const {
	if(this->x == other.x && this->y == other.y) {
		return true;
	}
	else {
		return false;
	}

}

void Point2D::operator=(const Point2D& other)  {
	this->x = other.x;
	this->y = other.y;
}

Point2D Point2D::midPoint(Point2D p1, Point2D p2){
	return Point2D((p1.x + p2.x)/2,(p1.y + p2.y)/2);
}
Point2D Point2D::operator+(const Point2D& other){
	return Point2D(this->x + other.x, this->y + other.y);
}

Point2D Point2D::operator-(const Point2D& other){
	return Point2D(this->x - other.x, this->y - other.y);
}


Rectangle::Rectangle(){
	v1 = Point2D();
	v2 = Point2D();
	v3 = Point2D();
	v4 = Point2D();
}

Rectangle::Rectangle(int x1,int y1, int x2, int y2){
	v1 = Point2D(x1, y1);
	v2 = Point2D(x2, y1);
	v3 = Point2D(x1, y2);
	v4 = Point2D(x2, y2);
}

Rectangle::Rectangle(Point2D v1, Point2D v4){
	this->v1 = v1;
	this->v4 = v4;
	v2 = Point2D(v4.x, v1.y);
	v3 = Point2D(v1.x, v4.y);
}

bool Rectangle::contains(Point2D p){
	if((p.x > v1.x)&&(p.x < v4.x)){
		return ((p.y > v1.y)&&(p.y < v4.y));
	}
	else
		return false;
}

bool Rectangle::border(Point2D p){
	if(p.x == v1.x || p.x == v4.x){
		return (p.y > v1.y)&&(p.y < v4.y);
	}
	if(p.y == v1.y || p.y == v4.y){
		return (p.x > v1.x)&&(p.x < v4.x);
	}
	return false;
}

bool Rectangle::intersects(Rectangle rect){
	if((v1 == rect.v1) && (v4 == rect.v4))
		return true;
	if(border(rect.v1)){
		if(border(rect.v2)){
			if(contains(Point2D::midPoint(rect.v1, rect.v2)))
				return true;
		}
		if(border(rect.v3)){
			if(contains(Point2D::midPoint(rect.v1, rect.v3)))
				return true;
		}
	}
	if(border(rect.v4)){
		if(border(rect.v2)){
			if(contains(Point2D::midPoint(rect.v4, rect.v2)))
				return true;
		}
		if(border(rect.v3)){
			if(contains(Point2D::midPoint(rect.v4, rect.v3)))
				return true;
		}
	}

	return contains(rect.v1) || contains(rect.v2) || contains(rect.v3) || contains(rect.v4);
}

bool Rectangle::Interscetion(Rectangle r1, Rectangle r2){
	return r1.intersects(r2) || r2.intersects(r1);
}

void Rectangle::print(){
	cout << "v1: " << v1.x << "," << v1.y << "\n";
	cout << "v2: " << v2.x << "," << v2.y << "\n";
	cout << "v3: " << v3.x << "," << v3.y << "\n";
	cout << "v4: " << v4.x << "," << v4.y << "\n";
}

/**
 * Matrix
 */
